<template>
  <view class="grid-container">
    <view class="custom-grid">
      <view v-for="(listItem, listIndex) in gridList" :key="listIndex" class="grid-item" @click="handleClick(listItem)">
        <view class="grid-item-content" :class="{ 'active': activeIndex === listIndex }" @touchstart="setActive(listIndex)" @touchend="clearActive">
          <image :src="listItem.name" class="grid-image"></image>
          <text class="grid-text">{{ listItem.title }}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { onMounted, ref } from 'vue';
export default {
  name: 'GridComponent',
  props: {
    gridList: {
      type: Array, 
      default: () => []
    }
  },
  emits: ['gridClick'],
  setup(props, { emit }) {
    const gridList = ref([]);
    onMounted(() => {
      gridList.value = props.gridList;
    });
    const activeIndex = ref(-1);
    const handleClick = (item) => {
      emit('gridClick', item);
    };
    const setActive = (index) => {
      activeIndex.value = index;
    };
    const clearActive = () => {
      setTimeout(() => {
        activeIndex.value = -1;
      }, 150);
    };
    return {
      gridList,
      activeIndex,
      handleClick,
      setActive,
      clearActive
    }
  }
}
</script>

<style lang="scss" scoped>
/* 宫格布局优化 */
.grid-container {
  background-color: #fff;
  border-radius: 12px;
  margin-top: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 20rpx;
}

/* 自定义网格布局 */
.custom-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20rpx;
  width: 100%;
}

.grid-item {
  display: flex;
  justify-content: center;
  align-items: center;
}

.grid-item-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20rpx 10rpx;
  width: 100%;
  min-height: 120rpx;
  transition: all 0.3s ease;
  border-radius: 12rpx;
  background: linear-gradient(135deg, #f8f9fa 0%, #ffffff 100%);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  border: 1px solid #f0f0f0;
  
  &.active {
    transform: scale(0.95);
    opacity: 0.8;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
  }
  
  &:hover {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
    transform: translateY(-2rpx);
  }
}

.grid-image {
  width: 80rpx;
  height: 80rpx;
  margin-bottom: 8rpx;
  transition: transform 0.3s ease;
}

.grid-item-content:hover .grid-image {
  transform: scale(1.1);
}

.grid-text {
  font-size: 20rpx;
  color: #666;
  text-align: center;
  line-height: 1.2;
  font-weight: 500;
  /* #ifndef APP-PLUS */
  box-sizing: border-box;
  /* #endif */
}
</style>